<?php

Yii::import('application.extensions.Api._base.BaseApiFunctionality');

class userApiFunctionality extends BaseApiFunctionality {
    /* --------------- Start  GET Actions --------------------- */

    function getUserDetail() {
        if ($this->validGETRequest()) {
            $mobile_number = '';

            //$user_id = ECOMAuthenticate::getObject()->getUserByAuthKey();
            //   if (!empty($user_id)) {
            if (isset($_GET['mobile_number']) && !empty($_GET['mobile_number'])) {

                $mobile_number = $_GET['mobile_number'];
            }

            if (empty($mobile_number)) {
                $data = new ECOMResult(412, ' Mobile number  is empty. Please send valid values and try again.');
            } else {

                $data = Yii::app()->db->createCommand()
                        ->select('users.mobile_number,users.status,profiles.firstname,profiles.image,profiles.date_of_birth,profiles.lastname,profiles.email,
                           ')
                        ->from('users')
                        ->join('profiles', 'users.id=profiles.user_id')
                        //->where((array('in', 'mobile_number', array(1, 2)))
                        ->where('mobile_number IN('.$mobile_number.')')//                          
                        ->queryAll();
                if (empty($data)) {
                    $data = new ECOMResult(412, 'Mobile number not exist.');
                    ECOMApiResponder::Respond($data, date('m-d-Y'));
                    die;
                }
            }
//            } else {
//                $data = new ECOMResult(401, 'User not authorized.');
//            }
        } else {
            $data = new ECOMResult(400, 'Invalid request parameters. Please check settings and try again.');
        }
        ECOMApiResponder::Respond($data, date('m-d-Y'));
    }

    /* --------------- End GET Actions ------------------------ */


    /* --------------- Start POST Actions --------------------- */

    function uploadPhoto() {
        if ($this->validPostRequest()) {
            $file_name = array();
            $mobile_no = "";
            //$user_id = ECOMAuthenticate::getObject()->getUserByAuthKey();

            //  if (!empty($user_id)) {

            if (!isset($_FILES['photo']) || empty($_FILES['photo'])) {
                $data = new ECOMResult(412, 'Photo is empty.');
            } elseif (!isset($_POST['mobile_no']) || empty($_POST['mobile_no'])) {
                $data = new ECOMResult(412, 'Mobile number is empty.');
            } else {
                $mobile_no = $_POST['mobile_no'];
                $user = Users::model()->find('mobile_number=:mobile_number', array(':mobile_number' => $mobile_no));

                if ($user) {
                    $profile = Profiles::model()->find('user_id=:user_id', array(':user_id' => $user->id));
                    $directoryPath = YiiBase::getPathOfAlias('webroot') . '/upload';
                    $path_parts = pathinfo($_FILES["photo"]["name"]);
                    $fileName = $mobile_no;
                    $fileTmpLoc = $_FILES["photo"]["tmp_name"];

                    if (move_uploaded_file($fileTmpLoc, $directoryPath . '/' . $fileName . '.' . $path_parts['extension'])) {
                        $profile->image = $fileName . '.' . $path_parts['extension'];
                        $profile->save(false);
                        $file_name[] = array('image' => Yii::app()->getBaseUrl(true) . '/upload/' . $fileName . '.' . $path_parts['extension']);
                        $data = $file_name;
                    } else {
                        $data = new ECOMResult(412, 'Photo not uploaded.');
                    }
                } else {
                    $data = new ECOMResult(412, 'Mobile number not exist.');
                    ECOMApiResponder::Respond($data, date('m-d-Y'));
                    die;
                }
                ECOMApiResponder::Respond($data, date('m-d-Y'));
                die;
            }
//            } else {
//                $data = new ECOMResult(401, 'User not authorized.');
//            }
        } else {
            $data = new ECOMResult(400, 'Invalid request parameters. Please check settings and try again.');
        }
        ECOMApiResponder::Respond($data, date('m-d-Y'));
    }

    public function registration() {

        if ($this->validPostRequest()) {
            $mobile_no = '';
            $password = '';
            $first_name = '';
            $last_name = '';
            $profile_pic_url = '';
            $email = '';

            if (isset($_POST['mobile_no']) && $_POST['mobile_no'] != '') {
                $mobile_no = $_POST['mobile_no'];
            }

//            if (isset($_POST['password']) && $_POST['password'] != '') {
//                $password = $_POST['password'];
//            }
            if (isset($_POST['first_name']) && $_POST['first_name'] != '') {
                $first_name = $_POST['first_name'];
            }
            if (isset($_POST['last_name']) && $_POST['last_name'] != '') {
                $last_name = $_POST['last_name'];
            }
            if (isset($_POST['profile_pic_url']) && $_POST['profile_pic_url'] != '') {
                $profile_pic_url = $_POST['profile_pic_url'];
            }
            if (isset($_POST['email']) && $_POST['email'] != '') {
                $email = $_POST['email'];
            }

            if (empty($mobile_no)) {
                $data = new ECOMResult(412, 'Mobile number is empty. Please send valid values and try again.');
//            } elseif (empty($password)) {
//                $data = new ECOMResult(412, 'Password is empty. Please send valid values and try again.');
//            } elseif (empty($first_name)) {
                $data = new ECOMResult(412, 'First name is empty. Please send valid values and try again.');
            }
//elseif (empty($email)) {
            //              $data = new ECOMResult(412, 'Email is empty. Please send valid values and try again.');
            //     } 
            else {
                $user = Users::model()->find('mobile_number=:mobile_number', array(':mobile_number' => trim($mobile_no)));
                $model = (!empty($user)) ? $user : new Users();
                ////////////////////////////
                $realname = $first_name . ' ' . $last_name;
                //$myOFControl = new ofctrl();
                //$myOFControl->init(SERVER_URL, ADMIN_USER, ADMIN_PASS);
//echo 'gyan';die;
                //  if ($myOFControl->usr_adduser($mobile_no, $password, $email, $realname, false)) {

                $model->mobile_number = $mobile_no;
                $model->xmpp_username = $mobile_no;
                $model->password = MD5($password);
                $model->status = 1;
                $model->xmpp_password = $password;
                $model->setAttribute('activationKey', guid());
                if ($model->save(false)) {
                    $profile = Profiles::model()->find('user_id=:user_id', array(':user_id' => trim($model->id)));
                     $profile = (!empty($profile)) ? $profile : new Profiles();
                    $profile->user_id = $model->id;
                     $profile->firstname = $first_name;
                      $profile->lastname =  $last_name;
                    $profile->created_at = date("Y-m-d H:i:s");
                    //$profile->ofuser_username = $mobile_no;
                    $profile->save(false);
                    //  $activation_url = 'http://' . $_SERVER['HTTP_HOST'] . $this->createUrl('user/user/recovery', array("activationKey" => $model->activationKey, "username" => $model->username));
                    //  anotherEmailSend($_POST['Profiles']['email'], 'Your password change link', $activation_url);
                    $data = new ECOMResult(200, 'Registration successfull');
                }
                // }
                //////////////////////////////
            }
            //else {
            //                 $data = new ECOMResult(412, 'Mobile number already exists..');
            //           }
        } else {
            $data = new ECOMResult(501, 'Invalid request parameters. Please check settings and try again.');
        }
        ECOMApiResponder::Respond($data, date('m-d-Y'));
    }

    /* --------------- End POST Actions --------------------- */
}

?>